//导入同步JS
import regeneratorRuntime, {
  async
} from '../../utils/runtime.js'
//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    userInfo: {},
    uindex: null,
    UI: [
      {greeting: "欢迎回来!"}, // Chinese UI
      {greeting: "Welcome back!"},  // English UI
      {greeting: "お帰りなさい!"}
    ],
    list: [
      {
        id: 'timecard',
        name: ['打卡', 'Time Card', '打刻'],
        open: false,        
        pages: [
          {path: "normal", title: ["定位打卡", "Normal Checkin", '正常打卡']},
          {path: "history", title: ["打卡记录", "Check History", "打卡历史"]}
          ]
      }, {
        id: 'overwork',
        name: ['消息', 'Application', "各種申請"],
        open: false,        
        pages: [
          {path: "create", title: ["系统消息", "Apply Overwork", "残業申請"]},
          {path: "list", title: ["个人消息", "Apply vacation", "休暇申請"]}
          ]
      }
      // , {
      //   id: 'sample',
      //   name: ['申请审批', 'Improvement', "申請承認"],
      //   open: false,        
      //   pages: [
      //     {path: "index", title: ["子菜单", "Sub-menu", "サブメニュー"]}
      //     ]
      // }
    ],
    maskFlag_phone:1,
    ifphone:0,

  },
  //事件处理函数
  kindToggle: function (e) {        
    var id = e.currentTarget.id, list = this.data.list;
    for (var i = 0, len = list.length; i < len; ++i) {
      if (list[i].id == id) {
        list[i].open = !list[i].open
      } else {
        list[i].open = false
      }
    }
    this.setData({
      list: list
    });
  },
  onLoad: function (optinon) {
    console.log('onLoad')

    
    var that = this
    if (app.globalData.openid) {
      console.log("初始获取openid");
      that.checkPhone();
      that.setData({
        openid: app.globalData.openid,
        hasUserInfo: true
      })
      that.pddl();
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.login({
        success: async res => {
          //查询,设置登陆信息
          var ifopenid = await that.getOP(res.code);
          if (ifopenid == false) {
            console.log("请求获取openid失败！");
          }
          that.pddl();
        },
        fail: function () {
          wx.showToast({
            title: '系统提示:网络错误',
            icon: 'warn',
            duration: 1500,
          })
        }
      })
    }
  },
  onShow:function(){
    // 设置app语言的全局变量  
    var selectedLanguage = app.globalData.settings.language;
    console.log('Current Language:' + selectedLanguage + ' (0: ZH-ch 1: ENG)');
    this.setData({
      uindex: selectedLanguage    
    })
  },
    //根据用户CODE调接口获取OPENID
    getOP: function (e) {
      var that = this;
      return new Promise(function (resolve, reject) {
        var url = app.globalData.serverPath + 'miniuser/user/getOpenidByCode?code=' + e;
        console.log(url);
        wx.request({
          //header: { 'content-type': 'application/x-www-form-urlencoded;charset=gbk' },
          url: url,
          method: 'GET',
          success: function (res) {
            console.log(res);
            app.globalData.openid = res.data.data.openid;
            app.globalData.session_key = res.data.data.session_key;
            that.checkPhone();
            console.log("获取OPENID成功：" + res.data.data.openid);
            resolve(true);
          },
          fail: function (res) {
            console.log("获取OPENID失败：" + res.data);
            resolve(false);
          }
        })
      })
    },
      /**
   * 判断授权
   */
  pddl: function () {
    //根据Openid获取用户信息，如果有信息，自动登录，如果没有，则跳转到登录页
    wx.request({
      url: app.globalData.serverPath + 'miniuser/user/getUserInfo',
      data: {
        openid: app.globalData.openid,
      },
      success:function(res) {
        console.log(res.data.code);
        if(res.data.code=="00"&&res.data.data.phone!=null&&res.data.data.phone!='')
        {

        }else{
          wx.redirectTo({
            url: '../login/login',
          })
        }
        
      },fail:function(res) {
        console.log(res);
        wx.redirectTo({
          url: '../login/login',
        })
        
      }
    })


    // var that = this;
    // //判断是否已经有用户名了
    // console.log("判断授权")
    // wx.request({
    //   url: app.globalData.path + 'mini/user/getUserInfo',
    //   data: {
    //     openid: app.globalData.openid,
    //   },
    //   success: function (res) {
    //     var username=res.data.name;
    //     console.log("用户名："+res.data.name);
    //     if (username != null && username != '' && username != '微信用户') { //如果数据库有数据，则返回1
    //       that.setData({
    //         maskFlag_sq: 1,
    //         ifsq: 1,
    //       });
    //     } else { //如果数据库中没有，弹出授权框
    //       that.setData({
    //         maskFlag_sq: 0
    //       })
    //     }
    //   },
    // })
  },
  //检测是否已经获取了用户手机号
  checkPhone:async function (e) {
    var that = this;
    
    return 1;
    console.log("3333333333333333");
    // if (that.data.ifphone == 1) { //如果该字段是1，为已经有了
    // } else { //查询接口判断用户是否已经有手机号
    //   wx.request({
    //     url: app.globalData.serverPath + 'miniuser/user/getUserInfo',
    //     data: {
    //       openid: app.globalData.openid,
    //     },
    //     success: function (res) {
    //       console.log(res);
    //       if (res.data.data.phone != null && res.data.data.phone != '') { //如果数据库有数据，则返回1
    //         that.setData({
    //           ifphone: 1,
    //         })
    //         return 1;
    //       } else { //如果数据库中没有，弹出授权框
    //         that.setData({
    //           maskFlag_phone: 0,
    //         })
    //         return 0;
    //       }
    //     },
    //     fail: function (res) {
    //       console.log(res);
    //     }
    //   })
    // }
  },
  //获取手机号码--首次登录时用到
  getPhoneNumber: function (e) {
    var that = this;
    
    return 1;
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      console.log("session_key::::" + app.globalData.session_key);
      wx.request({
        url: app.globalData.serverPath + 'miniuser/user/getPhoneNumber',
        data: {
          openid: app.globalData.openid,
          session_key: app.globalData.session_key,
          encryptedData: e.detail.encryptedData,
          iv: e.detail.iv,
        },
        success: function (res) {
          that.setData({
            ifphone: 1,
          });
        }
      })
    }
    that.maskCtr();
  },
    //控制遮罩
    maskCtr: function () {
      this.setData({
        maskFlag_phone: 1,
      })
    },
})
